home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 9915 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  7.0 KB

  1. Path: goanna.cs.rmit.EDU.AU!not-for-mail
  2. From: ok@goanna.cs.rmit.EDU.AU (Richard A. O'Keefe)
  3. Newsgroups: comp.lang.ada,comp.lang.c++,comp.lang.c,comp.lang.modula3,comp.lang.modula2
  4. Subject: Re: Hungarian notation - whoops!
  5. Date: 5 Mar 1996 14:39:13 +1100
  6. Organization: Comp Sci, RMIT, Melbourne, Australia
  7. Message-ID: <4hgd11$4p4@goanna.cs.rmit.EDU.AU>
  8. References: <30C40F77.53B5@swsbbs.com> <4g9255$74s@goanna.cs.rmit.EDU.AU> <4gip1iINNjd@keats.ugrad.cs.ubc.ca> <4h6hlo$hqu@goanna.cs.rmit.EDU.AU> <4h7vgdINNmsh@anvil.ugrad.cs.ubc.ca>
  9. NNTP-Posting-Host: goanna.cs.rmit.edu.au
  10. X-Newsreader: NN version 6.5.0 #0 (NOV)
  11.  
  12. c2a192@ugrad.cs.ubc.ca (Kazimir Kylheku) writes:
  13. > >One legal input is one LEGAL input.
  14.  
  15. >Whether or not it is a legal input is implementation defined. 
  16.  
  17. >What is the _guaranteed_ lowest signed integer, LONG_MIN?
  18.  
  19. >From K&R2, page 257:
  20.  
  21. >    LONG_MIN    -2143483647    minimum value of long
  22.  
  23. >This is clearly not the most negative two's complement 32-bit integer.
  24.  
  25. >By feeding -214348364_8_ to the abs() function or the unary - operator, you are
  26. >exceeding the stated limit. Thus your code is not strictly compliant with the
  27. >standard. 
  28.  
  29. As you so rightly say, LONG_MIN is implementation defined.  The standard
  30. *bounds* it, but does not *define* it.  I am aware of the C concept of
  31. "strict conformance", and I am also aware that experts in comp.std.c have
  32. found it remarkably difficult to construct one that is not trivial.  Code
  33. that uses the implementation-defined value of LONG_MIN does not _violate_
  34. the standard; it will not be strictly compliant but that doesn't mean it
  35. isn't compliant.
  36.  
  37. >Look, two's complement gives you one extra negative number that you aren't
  38. >supposed to use.  A sign/magnitude representation gives you two zeros instead.
  39.  
  40. The absolutely crucial point here is that TWO ZEROS ARE NOT A PROBLEM,
  41. because BOTH of them act EXACTLY THE SAME for all arithmetic operations.
  42.  
  43. We have an exactly comparable situation with pointers.  A machine is allowed
  44. to have any number of representations of "the" null pointer, and I've used
  45. one where there were 2**31 representations of "null pointer".  That didn't
  46. cause any complications in my source code, because they all behaved just the
  47. way a null pointer is supposed to behave.
  48.  
  49. It is not the case that twos complement gives you a number that you are
  50. not _supposed_ to use.  I have read the Pascal and C standards with some
  51. care, and neither of the Pascal standards, nor the C standard, nor the
  52. C++ draft standard, says anything about not being supposed to use -INT_MAX-1.
  53.  
  54. > >I am concerned about writing reliable maintainable software at affordable
  55. > >cost.  I am only interested in hardware, languages, compilers, and so on
  56.  
  57. >Then don't expect variables to hold values outside of what is stated
  58. >in the standard. 
  59.  
  60. Oh ye infernal deities.  Can you not realise that my code does not rule the
  61. world?  If I call an operating-system routine, I have to put up with whatever
  62. it gives me back.  If my code is a library that can be called by other people,
  63. it does not suffice for me to say "if you pass -INT_MAX-1 you have done
  64. something stupid and don't blame _me_ if it doesn't work".  The point is that
  65. other people's variables WILL hold values outside what is guaranteed in the
  66. standard, and if my code breaks as a result >I< will be blamed.
  67.  
  68. Let's be absolutely clear about this:  the C standard guarantees only that
  69. int can hold -32767..+32767, and the Ada standard guarantess only that
  70. Integer can hold -32767..+32767.  If my code is run on a machine where the
  71. implementation-defined range of int/Integer is -2147483648..2147483647
  72. and my code crashes because someone passed in the value 1000000, I am NOT
  73. going to get away with saying "well, you shouldn't expect variables to hold
  74. values outside of what is stated in the standard", now will I?  Why should
  75. any other implementation-defined-legal value be different?
  76.  
  77. >Ada is a fine language. C is a fine language. They exist because of creative
  78. >individuals, who have different approaches to a similar problem.
  79.  
  80. No.  They have different approaches to DIFFERENT problems.
  81. C was created for small team use on small systems for small machines.
  82. Ada was created for large team use on large systems.
  83.  
  84. >Hiding the two's complement representation would require the generation of all
  85. >kinds of extra code. It would be to the detriment of programs that never go
  86. >near the ``largest negative value''. 
  87.  
  88. That is precisely what I have against 2s complement.
  89.  
  90. >It doesn't have to be written with reference to what machine you are using.
  91.  
  92. I did not say that it did.  It can be written so that it will work on
  93. any machine, and I showed how.  The point is that THE OBVIOUS METHOD DOESN'T
  94. WORK IN TWOS COMPLEMENT!
  95.  
  96. > >    int like_atoi(char *p) {
  97. > >        int sign = -1;
  98. > >        int n = 0;
  99. > >
  100. > >        if (*p == '+') p++; else
  101. > >        if (*p == '-') p++, sign = 1;
  102. > >
  103. > >        while (isdigit(*p)) n = n*10 - (*p++ - '0');
  104. > >        return n*sign;
  105. > >    }
  106.  
  107. >This program is poor, because it doesn't place restrictions on the input. If I
  108. >feed it the string '123412341452345254623542435', it will produce a valid
  109. >variable of type 'int', without signalling that an invalid value has been
  110. >input.  Two's complement is the least of your problems here.
  111.  
  112. >By the way, ints can have the range as low -32767 to 32767, which further
  113. >restricts the robustness of the code.
  114.  
  115. Oh COME *ON*.  Instead of answering the substantive point, which is that
  116. there is an obvious way of coding this obviously simplified example,
  117. but the obvious way DOES NOT WORK IN TWOS COMPLEMENT, you resort to dishonest
  118. debating tricks. Can you not recognise a simplified example when you see one?
  119.  
  120. My actual code _does_ check ranges, and _is_ portable.
  121.  
  122. >If you write it properly, you could come up with a portable routine which works
  123. >for decimal representations of integers in the range LONG_MIN and LONG_MAX, and
  124. >signals an error condition for others. (I mean the *Standard's* minimum values
  125. >for LONG_MIN and LONG_MAX, of course, not implementation-defined values derived
  126. >from a local <limits.h>).
  127.  
  128. I have written it properly, elsewhere, and my code is portable, and does work
  129. for all legal inputs.  The point I have to keep hammering home is that the
  130. guts of the code, the statements that perform the actual calculations,
  131. CANNOT USE THE OBVIOUS METHOD BECAUSE IT DOESN'T WORK IN TWOS COMPLEMENT.
  132.  
  133. >Is this your prime evidence that C is bad or do you have more?
  134.  
  135. In all this disucssion I have not once stated or implied that >>C<< is bad.
  136. I have been complaining about TWOS COMPLEMENT ARITHMETIC, which is in no
  137. way synonymous with C.
  138.  
  139. For another example of the peculiarities of twos complement arithmetic,
  140. consider the fact that an arithmetic right shift on a ones complement or
  141. sign and magnitude machine is always equivalent to division by a power
  142. of 2, but not in a twos complement machine.  (I note that the C standard
  143. does not guarantee that there will be an arithmetic right shift, so please
  144. don't mistake this for a C criticism.)
  145.  
  146. -- 
  147. The election is over, and Australia lost; the idjits elected _politicians_!
  148. Richard A. O'Keefe; http://www.cs.rmit.edu.au/~ok; RMIT Comp.Sci.
  149.